<?php
class MYSQL
{
	protected $_link = null;

	public function __construct()
	{
	}

	public function connect($host, $username, $password, $dbname)
	{
		if (!$this->_link = mysql_connect($host, $username, $password))
		{
			exit('Can not connect to the database!');
		}

		if (!mysql_select_db($dbname, $this->_link))
		{
			exit('Can not select database: ' . $dbname . '!');
		}

		mysql_query("SET NAMES 'utf8'");
	}

	public function escape($data)
	{
		return mysql_real_escape_string($data, $this->_link);
	}

    public function query($sql)
    {
        return mysql_query($sql);
    }

    public function fetch($result, $fetchType = Database::FETCH_ASSOC)
	{
        switch ($fetchType)
        {
            case Database::FETCH_BOTH:
                $fetchType = MYSQL_BOTH;
            break;
            case Database::FETCH_NUM:
                $fetchType = MYSQL_NUM;
            break;
            default:
                $fetchType = MYSQL_ASSOC;
            break;
        }

		if ($result)
		{
			$rows = array();

			while ($row = mysql_fetch_array($result, $fetchType))
			{
				$rows[] = $row;
			}

			mysql_free_result($result);

			return $rows;
		}
		else
		{
			return false;
		}
	}

    public function getInsertId()
    {
        return mysql_insert_id();
    }

	public function close()
	{
		if ($this->_link != null)
		{
			mysql_close($this->_link);
		}
	}
}